
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;
using BCEntity.TMS.MallInterface.MallOrderAddress;

namespace BCData.TMS.MallInterface.MallOrderAddress
{
    public class MallOrderAddressData : IMallOrderAddressData
    {
        private readonly IDatabaseContext database;
        private readonly string QUERY = @"SELECT
		    A.MallOrderAddressId,
		    A.MallOrderNo,
		    A.SRType,
		    A.MallShopNo,
		    A.MallShopName,
		    A.CompanyName,
		    A.ContactName,
		    A.Tel,
		    A.Mobile,
		    A.ProvinceCode,
		    A.ProvinceName,
		    A.CityCode,
		    A.CityName,
		    A.CountyCode,
		    A.CountyName,
		    A.Address,
		    A.Lng,
		    A.Lat
        FROM
            MallOrderAddress A";



        #region 构造函数
        public MallOrderAddressData(IDatabaseContext database)
        {
            this.database = database;
        }
        #endregion

        #region InsertAsync
        /// <summary>
        /// 添加新商城订单地址纪录
        /// </summary>
        /// <param name="mallOrderAddressEntity"></param>
        /// <returns></returns>
        public async Task<MallOrderAddressEntity> InsertAsync(MallOrderAddressEntity entity)
        {
            return await this.database.Master.InsertAsync(entity, database.Transaction);
        }
        #endregion

        #region updateAsync
        /// <summary>
        /// 修改商城订单地址记录
        /// </summary>
        /// <param name="mallOrderAddressEntity"></param>
        /// <returns></returns>
        public async Task<MallOrderAddressEntity> UpdateAsync(MallOrderAddressEntity entity)
        {
            return await this.database.Master.UpdateAsync(entity, database.Transaction);
        }
        #endregion 

        #region Delete
        /// <summary>
        /// 删除商城订单地址记录
        /// </summary>
        /// <param name="mallOrderAddressId"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(long mallOrderAddressId)
        {
            string sql = "DELETE FROM mallorderaddress WHERE MallOrderAddressId = ?mallOrderAddressId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?MallOrderAddressId", mallOrderAddressId, DbType.Int64);
            return await this.database.Master.ExecuteAsync(sql, parameters, database.Transaction) > 0;
        }
        #endregion

        #region DeleteByMallOrderNo
        /// <summary>
        /// 删除商城订单地址记录
        /// </summary>
        /// <param name="mallOrderAddressId"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(string mallOrderNo)
        {
            string sql = "DELETE FROM mallorderaddress WHERE MallOrderNo = ?MallOrderNo";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?MallOrderNo", mallOrderNo, DbType.AnsiString);
            return await this.database.Master.ExecuteAsync(sql, parameters, database.Transaction) > 0;
        }
        #endregion

        #region GetById
        /// <summary>
        /// 获取商城订单地址记录
        /// </summary>
        /// <param name="mallOrderAddressId"></param>
        /// <returns></returns>
        public async Task<MallOrderAddressEntity> GetEntityAsync(long mallOrderAddressId)
        {
            string sql = QUERY + " WHERE  A.MallOrderAddressId = ?mallOrderAddressId";

            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?MallOrderAddressId", mallOrderAddressId, DbType.Int64);
            return await this.database.Slave.QueryEntityAsync<MallOrderAddressEntity>(sql, parameters);
        }
        #endregion

        #region GetByOrderNo
        /// <summary>
        /// 获取商城订单地址记录
        /// </summary>
        /// <param name="mallOrderAddressId"></param>
        /// <returns></returns>
        public async Task<IEnumerable<MallOrderAddressEntity>> GetAddressListAsync(string mallOrderNo)
        {
            string sql = QUERY + " WHERE  A.MallOrderNo = ?MallOrderNo";

            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?MallOrderNo", mallOrderNo, DbType.AnsiString);
            return await this.database.Slave.QueryAsync<MallOrderAddressEntity>(sql, parameters);
        }
        #endregion

        #region GetListAsync 查询商城订单地址记录
        public async Task<(IEnumerable<MallOrderAddressEntity>, int)> GetListAsync(int pageIndex, int pageSize)
        {

            DynamicParameters parameters = new DynamicParameters();
            //查询条件
            List<string> wheres = new List<string>();


            string sql = string.Format(@"{0} {1} ; SELECT COUNT(1) FROM  mallorderaddress  {1}",
                   QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize
                  );

            return await this.database.Slave.QueryPageableListAsync<MallOrderAddressEntity>(sql, parameters);
        }
        #endregion

    }
}
//接口




